local lianyingh = fk.CreateSkill{
  name = "hanqing__lianyingh",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__lianyingh"] = "连影",
  [":hanqing__lianyingh"] = "锁定技，当你失去装备区的牌后，你获得一张【影】；当你失去【影】后，你移动场上一张牌。若你的体力值为1，交换发动条件。",

  ["#hanqing__lianyingh-move"] = "连影：请移动场上一张牌",
}

local U = require "packages/utility/utility"

lianyingh:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(lianyingh.name) then return end
    local all_choices = {}
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            table.insertIfNeed(all_choices, 1)
          end
          if Fk:getCardById(info.cardId).trueName == "shade" then
            table.insertIfNeed(all_choices, 2)
          end
        end
      end
    end
    if #all_choices == 0 then return end
    local choices = {}
    if (player.hp ~= 1 and table.contains(all_choices, 1)) or (player.hp == 1 and table.contains(all_choices, 2)) then
      table.insert(choices, 1)
    end
    if #player.room:canMoveCardInBoard() > 0 then
      if (player.hp == 1 and table.contains(all_choices, 1)) or (player.hp ~= 1 and table.contains(all_choices, 2)) then
        table.insert(choices, 2)
      end
    end
    if #choices > 0 then
      event:setCostData(self, {choices = choices})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = table.simpleClone(event:getCostData(self).choices)
    if table.contains(choices, 1) then
      room:moveCardTo(U.getShade(room, 1), Card.PlayerHand, player, fk.ReasonJustMove, lianyingh.name, nil, true, player)
    end
    if table.contains(choices, 2) then
      if player.dead or #room:canMoveCardInBoard() < 2 then return end
      local targets = room:askToChooseToMoveCardInBoard(player, {
        skill_name = lianyingh.name,
        prompt = "#hanqing__lianyingh-move",
        cancelable = false,
      })
      room:askToMoveCardInBoard(player, {
        target_one = targets[1],
        target_two = targets[2],
        skill_name = lianyingh.name,
      })
    end
  end,
})

return lianyingh
